package com.suning.sawp.service.impl.dao.mainpushnew;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.maindiscode.MainDiscodeDto;
import com.suning.sawp.intf.dao.mainpushnew.MainPushTaskHqHandleDao;
import com.suning.sawp.po.mainpushnew.AreaModelRel;
import com.suning.sawp.po.mainpushnew.AreaTask;
import com.suning.sawp.po.mainpushnew.HqTask;
import com.suning.sawp.po.mainpushnew.Model;
import com.suning.sawp.po.mainpushnew.ModelCmmdty;
import com.suning.sawp.po.store.StoreBaseInfo;
import com.suning.sawp.service.util.BeanUtil;
import com.suning.sawp.vo.mainpushnew.HqModelAllocateVo;
import com.suning.sawp.vo.mainpushnew.HqTaskVo;
import com.suning.sawp.vo.mainpushnew.TaskModelVo;

/**
 * 
 * 总部主推任务维护dao实现<br>
 *
 * @author 14060886 祝翔龙
 */
@Repository
public class MainPushTaskHqHandleDaoImpl implements MainPushTaskHqHandleDao {

	@Resource
	private DalClient dalClient;

	public List<HqTask> queryPageHqTask(Map<String, Object> paramsMap) {
		return dalClient.queryForList("mainPushHqHandle.queryPageHqTask", paramsMap, HqTask.class);
	}

	public int countPageHqTask(Map<String, Object> paramsMap) {
		return dalClient.queryForObject("mainPushHqHandle.countPageHqTask", paramsMap, Integer.class);
	}

	@Override
	public HqTaskVo selectHqTaskInfoById(Long hqTaskId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		return dalClient.queryForObject("mainPushHqHandle.selectHqTaskInfoById", params, HqTaskVo.class);
	}

	@Override
	public List<TaskModelVo> selectModelInfoByTask(Long hqTaskId, Map<String, Integer> pageParams) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.putAll(pageParams);
		params.put("hqTaskId", hqTaskId);
		return dalClient.queryForList("mainPushHqHandle.selectModelInfoByTask", params, TaskModelVo.class);
	}

	@Override
	public Integer countModelInfoByTask(Long hqTaskId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		return dalClient.queryForObject("mainPushHqHandle.countModelInfoByTask", params, Integer.class);
	}

	@Override
	public List<HqModelAllocateVo> selectAreaAllocateDetailByModel(Long hqTaskId, Long modelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("modelId", modelId);
		return dalClient.queryForList("mainPushHqHandle.selectAreaAllocateDetailByModel", params, HqModelAllocateVo.class);
	}

	@Override
	public void updateModelTargetByArea(HqModelAllocateVo item) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("areaTaskId", item.getAreaTaskId());
		params.put("modelId", item.getModelId());
		params.put("targetNum", item.getTargetNum());
		dalClient.execute("mainPushHqHandle.updateTargetByArea", params);
	}

	public HqTask findHqTaskByMonthAndCateCode(String month, String cateCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("month", month);
		params.put("cateCode", cateCode);
		return dalClient.queryForObject("mainPushHqHandle.findHqTaskByMonthAndCateCode", params, HqTask.class);
	}

	public List<HqTask> queryHqTaskByMonth(String month) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("month", month);
		return dalClient.queryForList("mainPushHqHandle.queryHqTaskByMonth", params, HqTask.class);
	}

	@Transactional
	public void deleteHqTaskById(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		dalClient.execute("mainPushHqHandle.deleteHqTaskById", params);
		dalClient.execute("mainPushHqHandle.deleteModelByHqTaskId", params);
		dalClient.execute("mainPushHqHandle.deleteModelCmmdtiesByHqTaskId", params);
		dalClient.execute("mainPushHqHandle.deleteAreaTaskByHqTaskId", params);
		dalClient.execute("mainPushHqHandle.deleteAreaModelRelByHqTaskId", params);
		dalClient.execute("mainPushHqHandle.deleteStoreTaskByHqTaskId", params);
		dalClient.execute("mainPushHqHandle.deleteStoreModelRelByHqTaskId", params);
		dalClient.execute("mainPushHqHandle.deleteStaffModelRelByHqTaskId", params);
		dalClient.execute("mainPushHqHandle.DEL_BI_STORE_PUSH_TASK_MONTH_BY_TASK_ID", params);
		dalClient.execute("mainPushHqHandle.DEL_BI_STORE_PUSH_COMM_MONTH_BY_TASK_ID", params);
	}

	@Override
	public void deleteModeById(Long hqTaskId, Long modelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("modelId", modelId);
		dalClient.execute("mainPushHqHandle.deleteModeById", params);
	}

	@Override
	public void deleteModelCmmdtyRelById(Long hqTaskId, Long modelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("modelId", modelId);
		dalClient.execute("mainPushHqHandle.deleteModelCmmdtyRelById", params);
	}

	@Override
	public void deleteAreaModelRelById(Long hqTaskId, Long modelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("modelId", modelId);
		dalClient.execute("mainPushHqHandle.deleteAreaModelRelById", params);
	}

	@Override
	public void deleteStoreModelRelById(Long hqTaskId, Long modelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("modelId", modelId);
		dalClient.execute("mainPushHqHandle.deleteStoreModelRelById", params);
	}

	@Override
	public void deleteStaffModelRelById(Long hqTaskId, Long modelId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("modelId", modelId);
		dalClient.execute("mainPushHqHandle.deleteStaffModelRelById", params);
	}

	@Override
	public HqTask selectHqTaskById(String hqTaskId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		return dalClient.queryForObject("mainPushHqHandle.selectHqTaskById", params, HqTask.class);
	}

	@Override
	public Long insertModel(Model model) {
		Number number = dalClient.persist(model);
		if (number != null) {
			return number.longValue();
		}
		return 0L;
	}

	@Override
	public Integer insertModelCmmdty(ModelCmmdty modelCmmdty) {
		return dalClient.persist(modelCmmdty).intValue();
	}

	@Override
	public Integer hasAreaByCode(String disCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("disCode", disCode);
		return dalClient.queryForObject("mainPushHqHandle.hasAreaByCode", params, Integer.class);
	}

	@Override
	public Integer countArea() {
		return dalClient.queryForObject("mainPushHqHandle.countArea", new HashMap<String, Object>(), Integer.class);
	}

	@Override
	public Integer hasModelaByName(Long hqTaskId, String modelName) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("modelName", modelName);
		params.put("hqTaskId", hqTaskId);
		return dalClient.queryForObject("mainPushHqHandle.hasModelaByName", params, Integer.class);
	}

	@Override
	public void batchInsertAreaModelRel(List<AreaModelRel> rels) {
		@SuppressWarnings("unchecked")
		Map<String, Object>[] params = new Map[rels.size()];
		Map<String, Object> param = null;
		AreaModelRel rel = null;
		for (int i = 0; i < rels.size(); i++) {
			rel = rels.get(i);
			param = BeanUtil.describe(rel);
			params[i] = param;
		}
		dalClient.batchUpdate("mainPushHqHandle.batchInsertAreaModelRel", params);
	}

	@Override
	public void batchUpdateModel(List<Model> models) {
		for (Model model : models) {
			dalClient.dynamicMerge(model);
		}
	}

	@Override
	public void updateTaskRateById(Long hqTaskId, BigDecimal rate) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("rate", rate);
		dalClient.execute("mainPushHqHandle.updateTaskRateById", params);
	}

	public void batchSaveHqTask(Map<String, Object>[] batchValues) {
		dalClient.batchUpdate("mainPushHqHandle.batchSaveHqTask", batchValues);
	}

	public Integer batchSaveModelCmmdtyRel(Map<String, Object>[] batchValues) {
		int[] result = dalClient.batchUpdate("mainPushHqHandle.batchSaveModelCmmdtyRel", batchValues);
		int r = 0;
		for (int i = 0; i < result.length; i++) {
			if (result[i] > 0) {
				r++;
			}
		}
		return r;
	}

	public List<MainDiscodeDto> queryAllArea() {
		return dalClient.queryForList("mainPushHqHandle.queryAllArea", null, MainDiscodeDto.class);
	}

	public List<Model> queryAllModelByMonth(String month) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("month", month);
		return dalClient.queryForList("mainPushHqHandle.queryModelByMonth", params, Model.class);
	}

	public void batchSaveAreaTask(Map<String, Object>[] batchValues) {
		dalClient.batchUpdate("mainPushHqHandle.batchSaveAreaTask", batchValues);
	}

	public void batchSaveAreaModelTask(Map<String, Object>[] batchValues) {
		dalClient.batchUpdate("mainPushHqHandle.batchSaveAreaModelTask", batchValues);
	}

	@Override
	public List<AreaTask> queryAreaTaskByHqTaskId(Long hqTaskId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		return dalClient.queryForList("mainPushHqHandle.queryAreaTaskByHqTaskId", params, AreaTask.class);
	}

	@Override
	public void updateModelTargetNum(Map<String, Object>[] batchValues) {
		dalClient.batchUpdate("mainPushHqHandle.updateModelTargetNum", batchValues);
	}

	public List<StoreBaseInfo> queryStoreByDisCode(String disCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("disCode", disCode);
		return dalClient.queryForList("mainPushHqHandle.queryStoreByDisCode", params, StoreBaseInfo.class);
	}

	public void batchSaveStoreTask(String batchValues) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("batchValues", batchValues);
		dalClient.execute("mainPushHqHandle.batchSaveStoreTask", params);
	}

	public void updateHqTaskStaus(Long hqTaskId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		dalClient.execute("mainPushHqHandle.updateHqTaskStaus", params);
	}

	@Override
	public void updateModelTargetByCode(Long modelId, Long hqTaskId, Integer targetNum) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hqTaskId", hqTaskId);
		params.put("modelId", modelId);
		params.put("targetNum", targetNum);
		dalClient.execute("mainPushHqHandle.updateModelTargetByCode", params);
	}

}
